package bg.openid;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.openid4java.discovery.DiscoveryInformation;
import org.openid4java.message.Parameter;
import org.openid4java.message.ParameterList;

public class Bean {

	private String comment = "Comment bean";
	private String identifier = "";

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Bean() {

	}

	public String toString() {
		return "Bg Bean ";
	}

	public void addComment(String string) {
		this.comment += "\n<br/>----------------<br/> " + string;

	}

	public void addComment(String k, Object s) {
		this.addComment("<span style='color: red; font-weight: bolder;'> " + k + " </span>" + s);

	}

	public void addException(Throwable t) {
		comment += "<br/>-------------- <br/>";
		comment += " Exception: " + t.getClass() + "<br/>";
		comment += " " + t.getMessage() + "<br/>";
		StackTraceElement[] stes = t.getStackTrace();
		for (StackTraceElement ste : stes) {
			comment += "<br/>" + ste;
		}
		if (t.getCause() == null) {
			comment += " <br/> ---------------------";
		} else {
			comment += " <p/> cause by: ";
			addException(t.getCause());
		}
	}

	@SuppressWarnings("unchecked")
	public void display(ParameterList openidResp) {

		List parameters = openidResp.getParameters();
		Iterator iterator = parameters.iterator();
		while (iterator.hasNext()) {
			Parameter parameter = (Parameter) iterator.next();
			addComment("ParameterList :: " + parameter.getKey(), parameter.getValue());

		}

	}

	public void display(Map parameterMap) {
		for (Object o : parameterMap.entrySet()) {
			Object v = parameterMap.get(o);
			this.addComment("" + o, "" + v);
		}
	}

	public void displayUrl(String destinationURL) {
		try {
			StringTokenizer st1 = new StringTokenizer(destinationURL, "?");
			this.addComment("destinationURL.host", st1.nextToken());
			String params = st1.nextToken();
			params = URLDecoder.decode(params);
			StringTokenizer st2 = new StringTokenizer(params, "&");

			while (st2.hasMoreTokens()) {
				try {
					String s = st2.nextToken();
					StringTokenizer st3 = new StringTokenizer(s, "=");
					String key = st3.nextToken();
					String param = st3.nextToken();
					param = URLDecoder.decode(param);
					this.addComment("destinationURL.param <span style='color: blue; font-weight: bolder;'> " + key + "</span> " + param);
				} catch (Exception e) {
					this.addComment("ExeceptionAAA");
				}
			}
		} catch (Exception e) {
			this.addComment("ExeceptionBBB");
		}
	}

	public void display(List<DiscoveryInformation> discoveries) {
		for (DiscoveryInformation di : discoveries) {
			this.addComment("DiscoveryInformation ", " |  isVersion2:" + di.isVersion2() + " | DelegateIdentifier:" + di.getDelegateIdentifier() + " | ClaimedIdentifier:" + di.getClaimedIdentifier());
		}
	}

	public void display(HttpServletRequest request) {
		this.addComment("<hr/>DisplayRequest   <hr/> ");
		int i=0;
		Enumeration enu = request.getParameterNames();
		while(enu.hasMoreElements()){
			String k = (String) enu.nextElement();
			String v = request.getParameter(k);
			this.addComment("  param: "+k, v);
			i++;
		}
		this.addComment("Nb Params:"+i+"<hr/> ");
	}
}
